package com.gali.rabbit.seven;

import com.gali.rabbit.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Auther: gali
 * @Date: 2022-08-02 16:58
 * @Description:主题交换机
 */
public class ReceiveLogsTopic02 {
    public static final String EXCHANGE_NAME="topic_logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME,"topic");
        String queueName="Q2";
        channel.queueDeclare(queueName,false,false,false,null);
        channel.queueBind(queueName,EXCHANGE_NAME,"*.*.rabbit");
        channel.queueBind(queueName,EXCHANGE_NAME,"lazy.#");
        System.out.println("等待接收消息.......");
        //接收消息
        DeliverCallback deliverCallback=(consumerTag, message)->{
            System.out.println("接收队列是："+queueName+"绑定键是："+message.getEnvelope().getRoutingKey());
            System.out.println("ReceiveLogsTopic01控制台打印接收到的消息："+new String(message.getBody(),"UTF-8"));
        };
        channel.basicConsume(queueName,true,deliverCallback,consumerTag->{});
    }
}
